import os
from django.conf import settings

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from external.cron_jobs.cron_jobs_daemon import *
from models import *


class CronCommunication(BaseCron):
	def __init__(self, pid):
		BaseCron.__init__(self, pid)
		self.add_event("exec_communication", 1, "day", round=True)

	def exec_communication(self):
		communications = Communication.objects.filter(auto=True)
		for communication in communications:
			communication.send_all()

if __name__ == "__main__":

	daemon = CronCommunication(os.path.join(os.path.split(DIR)[0], 'django-cron-daemon-communiation.pid'))

	if len(sys.argv) == 2:
		if 'start' == sys.argv[1]:
			daemon.start()
		elif 'stop' == sys.argv[1]:
			daemon.stop()
		elif 'restart' == sys.argv[1]:
			daemon.restart()
		elif 'run' == sys.argv[1]:
			daemon.run()
		else:
			print "Unknown command"
			sys.exit(2)
			sys.exit(0)
	else:
		print "usage: %s start|stop|restart|run" % sys.argv[0]
		sys.exit(2)
